package com.ovhoo.vdm.vdm;

import java.io.IOException;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class VDMParser {
	
	private static final String tag_item_name = "item";
	private static final String tag_comment_name = "comment";
	private static final String attribute_id_name = "id";
	private static final String attribute_id_type = "integer";
	
	private static final String tag_author_name = "author";
	private static final String tag_category_name = "category";
	private static final String tag_date_name = "date";
	private static final String tag_agree_name = "agree";
	private static final String tag_deserved_name = "deserved";
	private static final String tag_comments_name = "comments";
	private static final String tag_text_name = "text";
	private static final String tag_comments_flag_name = "comments_flag";
	
	private final static SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");

	 
	public static ArrayList<VDMQuote> parseString(String string){
		
		final XmlPullParserFactory factory;
		
		final ArrayList<VDMQuote> quotes = new ArrayList<VDMQuote>();
		VDMQuote lastQuote = null;
		VDMQuoteComment lastComment = null;
		String currentText = "";

        
		try {
			factory = XmlPullParserFactory.newInstance();
		
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();
			
			xpp.setInput( new StringReader ( string) );
			int eventType = xpp.getEventType();
			
			while (eventType != XmlPullParser.END_DOCUMENT) {
				
				if(eventType == XmlPullParser.START_DOCUMENT) {
					System.out.println("VDMParser::parseString START_DOCUMENT");
					
					
					currentText = "";
				} else if(eventType == XmlPullParser.END_DOCUMENT) {
					System.out.println("VDMParser::parseString END_DOCUMENT");
					currentText = "";
				} else if(eventType == XmlPullParser.START_TAG) {
					System.out.println("VDMParser::parseString START_TAG " +  xpp.getName());
					
					String tag_name = xpp.getName();
					
					if (tag_name.equals( VDMParser.tag_item_name) ){
						lastQuote = new VDMQuote();
					  	 
						for (int index =0; index < xpp.getAttributeCount(); index++){
							String _tt = xpp.getAttributeName(index);
							if ( xpp.getAttributeName(index).equals(VDMParser.attribute_id_name) ){	 
					  			lastQuote.setId(xpp.getAttributeValue(index));
					  		 }
					  		 
					  	 }
					}
					else if (tag_name.equals( VDMParser.tag_comment_name) ){
						lastComment = new VDMQuoteComment();
						
						for (int index =0; index < xpp.getAttributeCount(); index++){
							String _tt = xpp.getAttributeName(index); 
							if ( xpp.getAttributeName(index).equals(VDMParser.attribute_id_name) ){	 
						  			lastComment.setId(xpp.getAttributeValue(index));
					  		}
					  	}
						
					}
					
					currentText = "";
				
				} else if(eventType == XmlPullParser.END_TAG) {
					//System.out.println("VDMParser::parseString END_TAG" + xpp.getName());
					
					String tag_name = xpp.getName();
					
					
					if (lastQuote != null){
						if (tag_name.equals( VDMParser.tag_item_name)){
							quotes.add(lastQuote);
							lastQuote = null;
						}
						else if ( tag_name.equals( VDMParser.tag_author_name)){
							lastQuote.setAuthor(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_date_name)){
							lastQuote.setDate(dateParser.parse(currentText));
						}
						else if ( tag_name.equals( VDMParser.tag_agree_name)){
							lastQuote.setAgree(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_deserved_name)){
							lastQuote.setDeserved(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_comments_name)){
							lastQuote.setNbComments(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_text_name)){
							lastQuote.setMessage(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_category_name)){
							lastQuote.setCategory(currentText);	
						}
						
					}
					else if (lastComment != null && quotes.size() > 0){
						if (tag_name.equals( VDMParser.tag_comment_name)){
							quotes.get(quotes.size()-1).addComment(lastComment);
							lastComment = null;
						}
						else if ( tag_name.equals( VDMParser.tag_author_name)){
							lastComment.setAuthor(currentText);	
						}
						else if ( tag_name.equals( VDMParser.tag_date_name)){
							lastComment.setDate(dateParser.parse(currentText));
						}
						else if ( tag_name.equals( VDMParser.tag_text_name)){
							lastComment.setText(currentText);	
						}
						
					}
					
					
					currentText = "";
				
				} else if(eventType == XmlPullParser.TEXT) {
					//System.out.println("VDMParser::parseString TEXT " + xpp.getText());
					
					 currentText += xpp.getText();

				}
				
				eventType = xpp.next();
			}
			
			return quotes;
		
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
		
	}
	
	
	

}
